﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace MrPainter
{
    class Line2D
    {
        public double x;
        public double y;

        public Line2D(double pos_x, double pos_y)
        {
            this.x = pos_x;
            this.y = pos_y;
        }

        public void polar (double mag, double ang_rad)
		{
			this.x = mag * Math.Cos(ang_rad);
			this.y = mag * Math.Sin(ang_rad);
		}

        public double getMagnitude()
        {
            return Math.Sqrt(this.x * this.x + this.y * this.y);
        }

        public void setMagnitude(double magnitude)
        {
            double current_ang = this.getAngle();
            this.polar(magnitude, current_ang);
        }

        public double getAngle()
        {
            return Math.Atan2(this.y, this.x);
        }

        public void setAngle(double ang_rad)
        {
            double current_mag = this.getMagnitude();
            this.polar(current_mag, ang_rad);
        }

        public void invert()
        {
            this.x *= -1;
            this.y *= -1;
        }

        public void invertX()
        {
            this.x *= -1;
        }

        public void invertY()
        {
            this.y *= -1;
        }

        public Line2D scale(double magnitude)
        {
            return new Line2D(this.x * magnitude, this.y * magnitude);
        }

        public Line2D rotate(double angle_rad)
        {
            double newX = this.x * Math.Cos(angle_rad) - this.y * Math.Sin(angle_rad);
            double newY = this.x * Math.Sin(angle_rad) + this.y * Math.Cos(angle_rad);

            return new Line2D(newX, newY);
        }

        public Line2D normalise()
        {
            double mag = this.getMagnitude();
            return new Line2D(this.x / mag, this.y / mag);
        }

        public double dotProduct(Line2D secondLine)
        {
            double componentX = this.x * secondLine.x;
            double componentY = this.y * secondLine.y;

            return componentX + componentY;
        }

        public double angleBetween(Line2D secondLine)
        {
            double dotProd = this.dotProduct(secondLine);
            double magProd = this.getMagnitude() * secondLine.getMagnitude();

            return (Math.Acos(dotProd / magProd) * (180 / Math.PI));
        }

        public double projectionOn(Line2D axis)
        {
            return this.dotProduct(axis.normalise());
        }

        public Line2D clone()
		{
			Line2D cloned = new Line2D(this.x, this.y);
			return cloned;
		}

    }
}
